// 用作所有缓存的文件

class LocalCache {
  // key 需要保存缓存的标识    value需要保存缓存值   isLocalSorage是否用localStorage缓存
  setCache(key: string, value: any, isLocalSorage = false) {
    if (isLocalSorage) {
      // console.log(value, '---我是要存的数据---')
      // console.log(typeof value, '---我是数据类型---')

      if (typeof value === 'object') {
        window.localStorage.setItem(key, JSON.stringify(value))
        return
      }

      window.localStorage.setItem(key, value)
    }
  }

  getCache(key: string, isLocalSorage = false) {
    let value = ''

    if (isLocalSorage) {
      value = window.localStorage.getItem(key)
    } else {
      value = window.sessionStorage.getItem(key)
    }

    if (value) {
      try {
        return JSON.parse(value)
      } catch (error) {
        return value
      }
    }
  }

  deleteCache(key: string, isLocalSorage = false) {
    if (isLocalSorage) {
      window.localStorage.removeItem(key)
      return
    }
    window.sessionStorage.removeItem(key)
  }

  // 全部删除
  clearCache() {
    window.localStorage.clear()
  }
}

export default new LocalCache()